package com.testdemo.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.labmanager.JsonModule.ResponseBase;
import com.testdemo.web.service.LabService;
import com.testdemo.web.service.MyUserService;
import com.testdemo.web.service.RecordService;
import com.testdemo.web.vo.LabInfo;
import com.testdemo.web.vo.MyUser;
import com.testdemo.web.vo.RecentRangement;
import com.testdemo.web.vo.Session;
import com.testdemo.web.vo.WeekSummary;

@Controller
@RequestMapping("/record")
public class RecordController {
	
	
	@Resource
	private RecordService recordService;
	@Resource
	private MyUserService myUserServiceImpl;
	@Resource
	private LabService labServiceImpl;
	
	@RequestMapping(value = "/addrecent", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase AddRecent(@RequestBody RecentRangement recentRangement) {
		ResponseBase responseBase = new ResponseBase();
		
		//检查数据完整性
		if(false == recentRangement.checkData()) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//检查登陆状况
		if(false == AccountController.sessionManager.CheckSessionAccessAble(recentRangement.getSession())) {
			responseBase.setError(-1, "请先登陆后再操作");
			return responseBase;
		}
		
		//插入一条数据
		if(recordService.addRecentArrangement(recentRangement) == 0) {
			responseBase.setError(-5, "增加失败");
			return responseBase;
		}
		
		return responseBase;
	}
	
	@RequestMapping(value = "/getrecent", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase GetRecent(@RequestBody MyUser myUser) {
		ResponseBase responseBase = new ResponseBase();
		//检验数据合法性
		if(!myUser.checkData()) {
			responseBase.setError(-1, "请求数据错误");
			return responseBase;
		}
		//检查用户是否登陆
		Session session = AccountController.sessionManager.selectSessionByAccessToken(myUser.getSession());
		if(null == session) {
			responseBase.setError(-1, "请先登陆");
			return responseBase;
		}
		//获取所属实验室管理员id
		MyUser user = myUserServiceImpl.selectUserByName(myUser.getUsername());
		LabInfo labInfo = labServiceImpl.selectLabinfoById(user.getLabid());
		
		//检验用户权限
		if(!session.getUsername().equals(myUser.getUsername()) && !labInfo.getUsername().equals(session.getUsername())) {
			responseBase.setError(-1, "你没有权限查看");
			return responseBase;
		}
		
		
		List<RecentRangement> list = recordService.getaddRecentArrangementByName(myUser.getUsername());
		
		responseBase.setData(list);
		
		return responseBase;
	}
	
	@RequestMapping(value = "/deleterecent", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase DeleteRecent(@RequestBody RecentRangement recentRangement) {
		ResponseBase responseBase = new ResponseBase();
		//检验数据
		if(recentRangement.getId() == null) {
			responseBase.setError(-5, "缺少必要参数");
			return responseBase;
		}
		
		//检验用户是否登陆
		Session session = AccountController.sessionManager.selectSessionByAccessToken(recentRangement.getSession());
		if(null == session) {
			responseBase.setError(-1, "请先登陆");
			return responseBase;
		}
		//检验该纪录是否为用户所有
		RecentRangement orginDate = recordService.getRecentRangementById(recentRangement.getId());
		if(!orginDate.getUsername().equals(session.getUsername())) {
			responseBase.setError(-1, "权限不足");
			return responseBase;
		}
		
		recordService.deleteRecentRangementById(recentRangement.getId());
		return responseBase;
	}
	
	
	
	@RequestMapping(value = "/addsummary", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase AddSummary(@RequestBody WeekSummary weekSummary) {
		ResponseBase responseBase = new ResponseBase();
		//数据验证
		if(false == weekSummary.checkData()) {
			responseBase.setError(-1, "请求数据错误");
			return responseBase;
		}
		//检查用户是否登陆
		Session session = AccountController.sessionManager.selectSessionByAccessToken(weekSummary.getSession());
		if(null == session) {
			responseBase.setError(-1, "请先登陆");
			return responseBase;
		}
		
		if(recordService.addWeekSummary(weekSummary) == 0) {
			responseBase.setError(-5, "增加失败");
			return responseBase;
		}
		
		return responseBase;
	}
	
	@RequestMapping(value = "/getsummary", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase GetSummary(@RequestBody MyUser myUser) {
		ResponseBase responseBase = new ResponseBase();
		//检验数据合法性
		if(!myUser.checkData()) {
			responseBase.setError(-1, "请求数据错误");
			return responseBase;
		}
		//检查用户是否登陆
		Session session = AccountController.sessionManager.selectSessionByAccessToken(myUser.getSession());
		if(null == session) {
			responseBase.setError(-1, "请先登陆");
			return responseBase;
		}
		//获取所属实验室管理员id
		MyUser user = myUserServiceImpl.selectUserByName(myUser.getUsername());
		LabInfo labInfo = labServiceImpl.selectLabinfoById(user.getLabid());
		
		//检验用户权限
		if(!session.getUsername().equals(myUser.getUsername()) && !labInfo.getUsername().equals(session.getUsername())) {
			responseBase.setError(-1, "你没有权限查看");
			return responseBase;
		}
		
		
		List<WeekSummary> list = recordService.getAllSummaryByUsername(myUser.getUsername());
		
		responseBase.setData(list);
		
		return responseBase;
	}
}
